﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BuzzwordBingo.Interface;

namespace BuzzwordBingo.Entities
{
    /// <summary>
    /// Bingo card, implements IBingoCard
    /// </summary>
    public class BingoCard : IBingoCard
    {
        #region constructors

        /// <summary>
        /// Initialize bingo card
        /// </summary>
        public BingoCard()
        {
            BuzzWords = new BuzzWord[5, 5];
            IsComplete = false;
        }

        #endregion

        #region IBingoCard Members

        public IBuzzWord[,] BuzzWords { get; set; }
        public bool IsComplete { get; set; }

        public void CheckCompleted()
        {
            IBuzzWord word;
            IsComplete = false;
            bool acrossMarked = true;
            bool downMarked = true;
            bool diagonalMarked1 = true;
            bool diagonalMarked2 = true;

            // check words completed across
            for (int k = 0; k < 5; k++)
            {
                for (int j = 0; j < 5; j++)
                {
                    word = BuzzWords[j, k];
                    acrossMarked &= word.IsMarked;
                }
                if (acrossMarked)
                {
                    IsComplete = true;
                    return;
                }
                else
                    acrossMarked = true;
            }

            // check words completed down
            for (int j = 0; j < 5; j++)
            {
                for (int k = 0; k < 5; k++)
                {
                    word = BuzzWords[j, k];
                    downMarked &= word.IsMarked;
                }
                if (downMarked)
                {
                    IsComplete = true;
                    return;
                }
                else
                    downMarked = true;
            }

            // check words completed in diagonals
            for (int i = 0; i < 5; i++)
            {
                word = BuzzWords[i, i];
                diagonalMarked1 &= word.IsMarked;
            }
            if (diagonalMarked1)
            {
                IsComplete = true;
                return;
            }

            for (int i = 0; i < 5; i++)
            {
                word = BuzzWords[4 - i, i];
                diagonalMarked2 &= word.IsMarked;
            }
            if (diagonalMarked2)
            {
                IsComplete = true;
                return;
            }        
        }

        public void MarkBuzzWord(int Index)
        {
            int j, k;

            j = Index % 5;
            k = Index / 5;

            IBuzzWord word = BuzzWords[j, k];
            if (word != null)
                word.IsMarked = true;

            CheckCompleted();
        }

        #endregion

        #region override functions

        /// <summary>
        /// Renders a string representation of a bingo card
        /// </summary>
        /// <returns>bingo card as string</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            bool addSeparator = false;
            IBuzzWord word;
            for (int k = 0; k < 5; k++)
            {
                for (int j = 0; j < 5; j++)
                {
                    word = BuzzWords[j, k];
                    if (addSeparator)
                        sb.Append("-");
                    else
                        addSeparator = true;

                    sb.Append(word.FuzzyWord);
                }
                sb.Append(Environment.NewLine);
                addSeparator = false;
            }

            return sb.ToString();
        }

        #endregion
    }
}
